package com.wandian.games.mvvm.bookactivity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.iBookStar.views.YmConfig;
import com.iBookStar.views.YmWebView;
import com.mylib.libcore.AppContext;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.utils.StatusBarUtils;
import com.wandian.games.R;
import com.wandian.games.databinding.GamesActivityBookBinding;
import com.wandian.games.mvvm.gamebook.GameBookFragment;


/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description WXTopActivity
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_GAMES_BOOK)
public class BookActivity extends BaseMVVMActivity<BookActivityViewModel>  {
    private GamesActivityBookBinding dataBinding;
    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.games_activity_book);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setViewModel(mViewModel);
        StatusBarUtils.with(this,false)
                .setIsActionBar(false)
                .clearActionBarShadow()
                .setDrawable(getResources().getDrawable(R.drawable.sys_color))
                .init();
        initViewData();

    }

    private void initViewData(){
        dataBinding.bindTransparentToolBar.toolBarTvTitle.setText("在线小说");
        dataBinding.bindTransparentToolBar.toolBarIvBack.setOnClickListener(v -> finish());
        //  fragments.add(new ReaderFragment());
//        mTransaction = mSupportFragmentManager.beginTransaction();
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_game_book, new GameBookFragment.ReaderFragment()).commitAllowingStateLoss();
    }

    //小说自定义Fragment
    public static class ReaderFragment extends Fragment {
        private YmWebView ymWebView;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //配置落地页TitleBar的背景颜色和文字颜色
            YmConfig.setTitleBarColors(0xffffffff, 0xff000000);
            YmConfig.setOutUserId(AppContext.getInstance().getAppPreferencesHelper().getKeyValue("user_id"));
            //YmConfig.setOutUserId("12345678");			//对接金币必须事先调用此接口
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            if(ymWebView == null) {
                //初始化YmWebView，即可打开书城
                ymWebView = new YmWebView(getActivity());
                //出现容器和h5页面上事件冲突时，通知h5
                ymWebView.disableSwiperTouch();
                ymWebView.openBookStore();
            }
            return ymWebView;
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
        }
    }

}
